//
//  TopicAuthorQrImageView.swift
//  Venus
//
//  Created by Smith_Yang on 2018/8/6.
//  Copyright © 2018 zhaowang. All rights reserved.
//

import UIKit

class TopicAuthorQrImageView: UIView {
    
    var model : TopicDetailModel?
    var imageViewList : [UIImageView] = [UIImageView]()
    
    public func configModel(_model : TopicDetailModel) {
        model = _model;
        
        setupUI()
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.4)
        
        if let authorAccount = model?.topicAuthorAccountModel {
            
            for item in authorAccount {
                
                let imageView = UIImageView(frame: CGRect.zero)
                imageView.showImageS(item.accountQrResource)
                imageView.tag = Int(item.accountType)!
                addSubview(imageView)
                imageViewList.append(imageView)
                
                let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longpressAction))
                imageView.addGestureRecognizer(longPressGesture)
                imageView.isUserInteractionEnabled = true
            }
        }
        
        let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        self.addGestureRecognizer(tapgesture)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if (imageViewList.count > 0){
            
            if (imageViewList.count == 1){
                let imgView = imageViewList.first
                imgView?.snp.makeConstraints({ (make) in
                    make.centerY.equalTo(self.snp.centerY)
                    make.width.equalTo(100)
                    make.height.equalTo(300)
                    make.centerX.equalTo(self.snp.centerX)
                })
            }
            else if (imageViewList.count == 2){
                let imgView = imageViewList.first
                imgView?.clipsToBounds = true
                imgView?.snp.makeConstraints({ (make) in
                    make.centerY.equalTo(self.snp.centerY)
                    make.width.equalTo(kScreenW/2 - 15)
                    make.height.equalTo(300)
                    make.left.equalTo(5)
                })
                
                let lastImageView = imageViewList.last
                lastImageView?.clipsToBounds = true
                lastImageView?.snp.makeConstraints({ (make) in
                    make.centerY.equalTo(self.snp.centerY)
                    make.width.equalTo(kScreenW/2 - 15)
                    make.height.equalTo(300)
                    make.left.equalTo((imgView?.snp.right)!).offset(5)
                })
            }
            
        }
    }
  
    @objc func tapAction(){
        if (superview != nil) {
            removeFromSuperview()
        }
    }
    
    @objc func longpressAction(_ gesture: UILongPressGestureRecognizer){
        
        
        guard let image = imageViewList[(gesture.view?.tag)!].image else {return}
        
        let detector: CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])!
        
        let img = CIImage(cgImage: (image.cgImage)!)
        
        let features: [CIFeature]? = detector.features(in: img, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
        
        if( features != nil && (features?.count)! > 0) {
            let feature = features![0]
            
            if feature.isKind(of: CIQRCodeFeature.self) {
                let featureTmp: CIQRCodeFeature = feature as! CIQRCodeFeature
                
                let scanResult = featureTmp.messageString
                
                if let url = URL(string: scanResult!) {
                    if (UIApplication.shared.canOpenURL(url)){
                        UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    }
                }
            }
        }
        
        if gesture.view?.tag == 0 {
            
            
        } else {
            
        }
        
    }
    

}
